package user

import (
	"math/rand"
	"strconv"
	"strings"
	"time"
)

var colorFindMa = map[string][]interface{}{
	"red":   {"01", "02", "07", "08", 12, 13, 18, 19, 23, 24, 29, 30, 34, 35, 40, 45, 46},
	"blue":  {"03", "04", "09", 10, 14, 15, 20, 26, 25, 31, 36, 37, 41, 42, 47, 48},
	"green": {"05", "06", 11, 16, 17, 21, 22, 27, 28, 32, 33, 38, 39, 43, 44, 49},
}

var colorFindMas = map[string][]interface{}{
	"red":   {"01", "02", "07", "08", 12, 13, 18, 19, 23, 24, 29, 30, 34, 35, 40, 45, 46},
	"blue":  {"03", "04", "09", 10, 14, 15, 20, 26, 25, 31, 36, 37, 41, 42, 47, 48},
	"green": {"05", "06", 11, 16, 17, 21, 22, 27, 28, 32, 33, 38, 39, 43, 44, 49},
}

var maFindXiao2023 = []string{
	"", "兔", "虎", "牛", "鼠", "猪", "狗", "鸡", "猴", "羊", "马", "蛇", "龙", "兔", "虎", "牛", "鼠",
	"猪", "狗", "鸡", "猴", "羊", "马", "蛇", "龙", "兔", "虎", "牛", "鼠", "猪", "狗", "鸡", "猴", "羊",
	"马", "蛇", "龙", "兔", "虎", "牛", "鼠", "猪", "狗", "鸡", "猴", "羊", "马", "蛇", "龙", "兔",
}

// GetZodiac 根据号码获取生肖
func GetZodiac(number int) string {
	zodiacIndex := number % len(maFindXiao2023)
	if zodiacIndex == 0 {
		zodiacIndex = len(maFindXiao2023)
	}
	return maFindXiao2023[zodiacIndex]
}

// 根据字符串数组获取生肖
func GetZodiacs(numbers []string) string {
	var zodiacs []string
	for _, number := range numbers {
		numberInt, _ := strconv.Atoi(number)
		zodiacs = append(zodiacs, GetZodiac(numberInt))
	}
	return strings.Join(zodiacs, ",")
}

// 生成生肖的去重数组
func generateZodiacs(numbers string) ([]string, int) {
	baseNumbers := strings.Split(numbers, ",")
	return removeRepByMapString(baseNumbers), len(removeRepByMapString(baseNumbers))
}

func removeRepByMapString(zodiacs []string) []string {

	result := []string{}
	tempMap := map[string]byte{} // 存放不重复主键
	for _, e := range zodiacs {
		l := len(tempMap)
		tempMap[e] = 0
		if len(tempMap) != l { // 加入map后，map长度变化，则元素不重复
			result = append(result, e)
		}
	}
	return result
}

// getCode 根据生肖获取号码
func getCode(zodiac string) int {
	for i, z := range maFindXiao2023 {
		if z == zodiac {
			return i
		}
	}
	return 0
}

// GetColor 根据号码获取颜色
func GetColor(number int) string {
	numberStr := strconv.Itoa(number)
	for color, numbers := range colorFindMa {
		for _, num := range numbers {
			switch num := num.(type) {
			case string:
				if num == numberStr {
					return color
				}
			case int:
				if num == number {
					return color
				}
			}
		}
	}
	return ""
}

// 获取特码
func generateSpecialCode(zodiacPrediction string) string {
	zodiacs := strings.Split(zodiacPrediction, ",")
	//获取特码
	specialCode := zodiacs[6]
	return specialCode
}

// 从1-49中随机生成7个不重复的数
func generateCode() []string {
	//从1-49中随机生成7个不重复的数
	var code []int
	for {
		if len(code) == 7 {
			break
		}
		rand.Seed(time.Now().UnixNano())

		// 生成随机整数
		randomInt := rand.Intn(49)
		code = append(code, randomInt+1)
		code = removeRepByMap(code)
	}
	//把生成的7个数1位的补成两位，例如1变成01的字符串
	var codeStr []string
	for _, v := range code {
		if v < 10 {
			codeStr = append(codeStr, "0"+strconv.Itoa(v))
		} else {
			codeStr = append(codeStr, strconv.Itoa(v))
		}
	}
	//把生成的7个数转成字符串以，拼接

	//判断数字转成生肖后是否是7⃣️个不同的生肖，如果是则重新生成
	if len(removeRepByMapString(strings.Split(GetZodiacs(codeStr), ","))) == 7 {
		return generateCode()

	} else {
		return codeStr
	}

	//for i, v := range code {
	//	if v < 10 {
	//		code[i] = v + 100
	//	}
	//}
	////把生成的7个数转成字符串
	//var codeStr []string
	//for _, v := range code {
	//	codeStr = append(codeStr, strconv.Itoa(v))
	//}
}

func removeRepByMap(code []int) []int {
	result := []int{}
	tempMap := map[int]byte{} // 存放不重复主键
	for _, e := range code {
		l := len(tempMap)
		tempMap[e] = 0
		if len(tempMap) != l { // 加入map后，map长度变化，则元素不重复
			result = append(result, e)
		}
	}
	return result
}

// 结构体定义
type Encour struct {
	ID       uint   `gorm:"primaryKey;autoIncrement"`
	Expect   string `json:"expect"`
	Opencode string `json:"opencode"`
	Opentime string `json:"opentime"`
	Status   int8   `json:"status"`
}

var maWx = map[string][]interface{}{
	"金": {1, 2, 9, 10, 23, 24, 31, 32, 39, 40},  //	虎、鸡
	"木": {5, 6, 13, 14, 21, 22, 35, 36, 43, 44}, //兔、猴
	"水": {11, 12, 19, 20, 27, 28, 41, 42, 49},   //鼠、猪
	"火": {7, 8, 15, 16, 29, 30, 37, 38, 45, 46}, //  	蛇、马
	"土": {3, 4, 17, 18, 25, 26, 33, 34, 47, 48}, //	牛、龙、羊、狗
}

// 根据单个号码获取对应的五行
func GetMaWxByNumber(number int) string {
	return getMaWx(number)
}

// GetMaWx 根据生成的字符串数组号码获取五行
func GetMaWx(numbers []string) string {
	var wxs []string
	for _, number := range numbers {
		numberInt, _ := strconv.Atoi(number)
		wxs = append(wxs, getMaWx(numberInt))
	}
	return strings.Join(wxs, ",")
}

func getMaWx(numberInt int) string {
	//获取五行
	for wx, numbers := range maWx {
		for _, num := range numbers {
			switch num := num.(type) {
			case string:
				if num == strconv.Itoa(numberInt) {
					return wx
				}
			case int:
				if num == numberInt {
					return wx
				}
			}
		}
	}
	return ""
}

// GetZodiacAllNumber 根据一个生肖获取对应的全部数字
func GetZodiacAllNumber(zodiac string) []string {
	var numbers []string
	for i := 1; i <= 49; i++ {
		if GetZodiac(i) == zodiac {
			numbers = append(numbers, strconv.Itoa(i))
		}
	}
	return numbers
}

// GetZodiacNumbers 根据生肖字符串数组获取号码
func GetZodiacNumbers(zodiacs string) []string {
	var numbers []string
	//把zodiacs字符串转换成数组
	zodiacsd := strings.Split(zodiacs, ",")
	for _, zodiac := range zodiacsd {
		numbers = append(numbers, GetZodiacAllNumber(zodiac)...)
	}
	return numbers
}

// 根据颜色获取对应的所有号码
func GetMaColorByColor(color string) []string {
	return getMaColor(color)
}

func getMaColor(color string) []string {
	var numbers []string
	for c, nums := range colorFindMas {
		if c == color {
			for _, num := range nums {
				switch num := num.(type) {
				case string:
					numbers = append(numbers, num)
				case int:
					numbers = append(numbers, strconv.Itoa(num))
				}
			}
		}
	}
	return numbers
}
